package com.yanqu.road.server.cmd.activity.dafuweng;

import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionRecord;
import com.yanqu.road.pb.monopoly.CrossMonopolyProto;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.pb.DaFuWengPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_DA_FU_WENG_UNION_RECORD, desc = "商会造谣、祝福记录")
public class DaFuWengRecordCmd extends AbstractCommand {

    // 1.造谣，2.祝福
    int type;

    List<DFWUnionRecord> unionRecord;

    private boolean needSend = true;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MonopolyProto.GetUnionRecordReqMsg reqMsg = MonopolyProto.GetUnionRecordReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
        if (dfwConfig == null || !ActivityMgr.activityInTime(dfwConfig.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_DFW_NO_IN_TIME;
            return;
        }
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid) || CrossUnionMgr.getUnionInfo(unionUid) == null) {
            ret = 0;
            // 无商会返回空数组
            unionRecord = new ArrayList<>();
            return;
        }

      //  unionRecord = DaFuWengMgr.getUnionRecord(dfwConfig.getActivityInfo().getActivityId(), unionUid, type);
        needSend = false;
        CrossMonopolyProto.CrossDfwUnionRecordReqMsg.Builder reqMsg = CrossMonopolyProto.CrossDfwUnionRecordReqMsg.newBuilder();
        reqMsg.setActivityId(dfwConfig.getActivityInfo().getActivityId());
        reqMsg.setType(type);
        reqMsg.setUnionId(unionUid);
        player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_DA_FU_WENG_UNION_RECORD, reqMsg, dfwConfig.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (needSend) {
            MonopolyProto.GetUnionRecordRespMsg.Builder builder = MonopolyProto.GetUnionRecordRespMsg.newBuilder();
            builder.setRet(ret);
           /* if (ret == 0) {
                List<MonopolyProto.UnionRecordMsg> msgList = DaFuWengPb.parseUnionRecordMsgList(unionRecord);
                builder.addAllRecordList(msgList);
            }*/
            player.sendPacket(ClientProtocol.U_DA_FU_WENG_UNION_RECORD, builder);
        }
    }
}
